//
//  CHttpManager.m
//  FMMarket
//
//  Created by zqt on 2020/8/4.
//  Copyright © 2020 dangfm. All rights reserved.
//

#import "CHttpManager.h"
#import <AFNetworking/AFNetworking.h>
@implementation CHttpManager

#pragma mark - post请求
+(void)requestPost:(NSString *)url andParam:(NSDictionary *)paramDic andSuccess:(successResultBlock)success andFailed:(faildResultBlock)failed
{
//    //MARK: GET
//    let realurlStr = url.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
//    let realUrl = URL(string: realurlStr!)
//    let request : NSMutableURLRequest = NSMutableURLRequest(url: realUrl!);
//    request.timeoutInterval = 10
//
//    if(method == CCHttpMethod.GET){
//        request.httpMethod = "GET"
//    }else if(method == CCHttpMethod.POST){
//        request.httpMethod = "POST"
//    }
//
//    let session = URLSession.shared
//    let httpTask = session.dataTask(with: request as URLRequest) { data, response, error in
//        if(error != nil){
//            DispatchQueue.main.async {
//                failedBlock(error! , nil)
//            }
//        }else{
//            do{
//                let result =  try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments)
//                if(result is Dictionary<String , Any>){
//                    DispatchQueue.main.async {
//                        completeBlock(result as! Dictionary<String , Any> )
//                    }
//                }else{
//                    DispatchQueue.main.async {
//                        failedBlock(nil , ["code":"10086"])
//                    }
//                }
//            }
//            catch{
//                DispatchQueue.main.async {
//                    failedBlock(nil , ["code":"10086"])
//                }
//            }
//        }
//    }
//    httpTask.resume()
//    kWeakSelf;
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:NotNilStr(url)]];
    request.timeoutInterval = 10;
    request.HTTPMethod = @"POST";
    [request setValue:@"lH7FB4lKVwzI92TFdW8mz5il-MdYXbMMI" forHTTPHeaderField:@"X-LC-Id"];
    [request setValue:@"5C7x7oOODY8cycyLOrD1kLtL" forHTTPHeaderField:@"X-LC-Key"];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionTask *task =  [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if(!error){
                NSDictionary *resultDic =  [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
                if([resultDic  isKindOfClass:[NSDictionary class]]){
                    success(resultDic);
                    return;
                }
            }
            failed(nil,@{@"msg":@"error"});
        });
    }];
    [task resume];
}
+(void)requestGET:(NSString *)url andParam:(NSDictionary *)paramDic andSuccess:(successResultBlock)success andFailed:(faildResultBlock)failed
{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:NotNilStr(url)]];
    request.timeoutInterval = 10;
    request.HTTPMethod = @"GET";
    [request setValue:@"lH7FB4lKVwzI92TFdW8mz5il-MdYXbMMI" forHTTPHeaderField:@"X-LC-Id"];
    [request setValue:@"5C7x7oOODY8cycyLOrD1kLtL" forHTTPHeaderField:@"X-LC-Key"];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionTask *task =  [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if(!error){
                NSDictionary *resultDic =  [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
                if([resultDic  isKindOfClass:[NSDictionary class]]){
                    dispatch_async(dispatch_get_main_queue(), ^{
                        success(resultDic);
                    });
                    return;
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                failed(nil,@{@"msg":@"error"});
            });
        });
    }];
    [task resume];
}
@end
